﻿using System.Windows;
using Byond.Frontend;
using NLog;

namespace Byond.Vst
{
	public class WpfHost
	{
		//private const uint WS_CHILD = 0x40000000;
		//private const uint WS_VISIBLE = 0x10000000;
		//private const int WM_GETDLGCODE = 0x0087;
		//private const int DLGC_WANTCHARS = 0x0080;
		//private const int DLGC_WANTTAB = 0x0002;
		//private const int DLGC_WANTALLKEYS = 0x0004;

		private readonly Logger _logger;
		private readonly FrameworkElement _control;
		//private readonly HwndSource _hwndSourceGcRoot;
		//private readonly FrameworkElement _wpfControlGcRoot;

		private Window _window;
		private Rect _lastWindowState = new Rect(0, 0, 1000, 900);

		public WpfHost(Logger logger, ByondControl control)
		{
			_logger = logger;
			_control = control;
			//_window = new Window
			//    {
			//        Content = control,
			//        Width = 800,
			//        Height = 700,
			//        ShowInTaskbar = false,
			//        //AllowsTransparency = true,
			//        //WindowStyle = WindowStyle.None,
			//        //Topmost = true
			//    };
			//var sourceParams = new HwndSourceParameters("MyWpfSourceWnd");

			//// This sets the position within the parent window
			//sourceParams.PositionX = 0;
			//sourceParams.PositionY = 0;
			//sourceParams.ParentWindow = parentHwnd;
			//sourceParams.WindowStyle = (int)(WS_VISIBLE | WS_CHILD);

			//var source = new HwndSource(sourceParams);
			//var wpfControl = control;

			//// This creates sets the root visual of the interop component to out user control.
			//// As a sidenote, THIS is where it crashes if you have forgotten to set STA-thread.
			//source.RootVisual = wpfControl;

			//// Adds a keyboard hook!
			//source.AddHook(ChildHwndSourceHook);

			//// This is important! If you do not save these the garbage collector eats them up!!
			//_hwndSourceGcRoot = source;
			//_wpfControlGcRoot = wpfControl;
		}

		//private IntPtr ChildHwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
		//{
		//    _logger.Info("Message: " + msg);
		//    if (msg == WM_GETDLGCODE)
		//    {
		//        _logger.Info("Ydljfhsdlf hsd hfdsjfs hjfhgfjhgfdkhkghfdkjghghfdgkjfdh");

		//        handled = true;
		//        // WANTCHARS = input boxes work
		//        // WANTTAB = tab order works
		//        // WANTALLKEYS = Enter key will work
		//        return new IntPtr(DLGC_WANTCHARS | DLGC_WANTTAB | DLGC_WANTALLKEYS);
		//    }

		//    return IntPtr.Zero;
		//}

		public void Show()
		{
			// we want to close the window if it was formerly closed by the user.
			if (_window != null)
			{
				if (!_window.IsLoaded)
				{
					Close();
				}
				else
				{
					_window.Activate();
					return;
				}
			}

			_window = new Window
			{
				Content = _control,
				Width = _lastWindowState.Width,
				Height = _lastWindowState.Height,
				Top = _lastWindowState.Top,
				Left = _lastWindowState.Left,
				ShowInTaskbar = false,
				AllowsTransparency = false,
				//WindowStyle = WindowStyle.ToolWindow,
				Topmost = true
			};

			_window.Activated += (s, e) => _window.Topmost = true;
			_window.Deactivated += (s, e) =>
				{
					_window.Topmost = false;
					_lastWindowState = new Rect(
						_window.Left,
						_window.Top,
						_window.ActualWidth,
						_window.ActualHeight);
				};

			_window.Show();
		}

		public void Close()
		{
			if (_window != null)
			{
				_window.Content = null;
				_window.Close();
				_window = null;
			}
		}
	}
}
